<?php

namespace plugins\word\controller;

use cmf\controller\PluginRestBaseController;
use think\Db;

class ApiIndexController extends PluginRestBaseController{
	
	
	public function index(){
		$param = $this->request->get();
		if(!empty($param['id'])){
			$id = $param['id'];
		}else{
			$id = 0;
		}
		$this->assign('id',$id);


		/*tree设置*/
		$tree = new Tree();
		$tree->icon = ['&nbsp;&nbsp;&nbsp;&nbsp│', '&nbsp;&nbsp;&nbsp;&nbsp;├', '&nbsp;&nbsp;&nbsp;&nbsp;└'];
		$list = $this->getList(cmf_get_current_admin_id());
		foreach ($list as $key => &$value) {
			if($value['type'] == '1'){
				$value['icon'] = "📁";
			}else{
				$value['disabled'] = "";
				$value['icon'] = "📄";
			}
			
			if($value['id'] == $id){
				$value['selected'] = "selected='selected'";
			}

		}
		$str = "<option value=\$id \$selected>\$spacer \$icon \$name</option>";
		/*tree设置*/

		//内容放入
		$tree->init($list);
		//tree生成
		$select =  $tree->getTree('0',$str);

		$this->assign('select',$select);
		return $this->fetch();
	}

	public function getList($param=null,$isModule=false){
		$data = $this->request->post();
		$data = zy_decodeData($data,$isModule);
		if(!empty($param)){
			$data = $param;
		}

		$list = db("word")->field('id,type,parent_id,name')->where(['private'=>0])->select()->toArray();

		if($list){
			return zy_array(true,'操作成功',$list,200,$isModule);
		}
		return zy_array(false,'操作失败',false,-2,$isModule);

	}

	public function wordContent($param=null,$isModule=false){
		$data = $this->request->post();
		$data = zy_decodeData($data,$isModule);
		if(!empty($param)){
			$data = $param;
		}

		$info = db('word')->where(['id'=>$data['id'],'private'=>0])->find();

		if($info){
			return zy_array(true,'操作成功',$info,200,$isModule);
		}
		return zy_array(false,'操作失败',false,-2,$isModule);

	}
	
	

}

?>